package com.ruoyi.fly.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.fly.domain.AppletServer;
import com.ruoyi.fly.domain.ServerCategory;
import com.ruoyi.fly.domain.vo.*;
import com.ruoyi.fly.service.AppletServerService;
import com.ruoyi.fly.service.ServerCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/serverCategory")
@Api(tags = "服务管理")
public class ServerCategoryController extends BaseController {

    @Autowired
    private ServerCategoryService serverCategoryService;

    @Autowired
    private AppletServerService appletServerService;


    /**
     * 查询服务分类列表
     */
    @ApiOperation(value = "查询服务分类列表")
    @PostMapping("/serverCategoryList")
    public R ServerCategoryList(@RequestBody ServerCategorySearchVo serverCategorySearchVo) {
        List<ServerCategory> list = serverCategoryService.selectServerCategoryPage(serverCategorySearchVo);
        if (list.size() == 0){
            return R.fail("暂无数据");
        }
        return R.ok(list);
    }

    @ApiOperation(value = "查询服务分类列表")
    @PostMapping("/selectList")
    public R selectList(@RequestBody ServerCategory serverCategory) {
        List<ServerCategory> list = serverCategoryService.selectServerCategoryList(serverCategory);
        return R.ok(list);
    }


    /**
     * 根据id获取服务分类详细信息
     */
    @ApiOperation(value = "根据id获取服务分类详细信息")
    @GetMapping(value = "/selServerCategoryById/{id}")
    public R selServerCategoryById(@PathVariable("id") String id) {
        if (serverCategoryService.selectServerCategoryById(id) == null){
            return R.fail("未查询到数据");
        }
        return R.ok(serverCategoryService.selectServerCategoryById(id));
    }

    /**
     * 新增服务分类
     */
    @ApiOperation(value = "新增服务分类")
    @PostMapping("/addServerCategory")
    public R addServerCategory(@RequestBody ServerCategory serverCategory) {
        return serverCategoryService.insertServerCategory(serverCategory);
    }

    /**
     * 修改服务分类
     */
    @ApiOperation(value = "修改服务分类")
    @PostMapping("/editServerCategory")
    public R editServerCategory(@RequestBody ServerCategory serverCategory) {
        return serverCategoryService.updateServerCategory(serverCategory);
    }

    /**
     * 删除服务分类
     */
    @ApiOperation(value = "删除服务分类")
    @PostMapping("/deleteServerCategory")
    public R removeServerCategory(@RequestBody DeleteVo deleteVo) {
        return serverCategoryService.deleteServerCategoryByIds(deleteVo.getIds());
    }

    /**
     * 查询服务列表
     */
    @ApiOperation(value = "查询服务列表")
    @PostMapping("/serverList")
    public TableDataInfo serverList(@RequestBody AppletServerSearchVo appletServerSearchVo) {
        PageDomain pageDomain = new PageDomain();
        BeanUtils.copyProperties(appletServerSearchVo,pageDomain);
        startPage(pageDomain);
        List<AppletServerVo> list = appletServerService.selectAppletServerList(appletServerSearchVo);
        return getDataTable(list);
    }

    /**
     * 根据id获取服务详细信息
     */
    @ApiOperation(value = "根据id获取服务详细信息")
    @GetMapping(value = "/selServerById/{id}")
    public R selServerById(@PathVariable("id") String id) {
        AppletServer appletServer = appletServerService.selectAppletServerById(id);
        if (appletServer ==null){
            return R.fail("无数据");
        }
        return R.ok(appletServerService.selectAppletServerById(id));
    }

    /**
     * 新增服务
     */
    @ApiOperation(value = "新增服务")
    @PostMapping("/addServer")
    public R addServer(@RequestBody AppletServer appletServer) {
        return appletServerService.insertAppletServer(appletServer);
    }

    /**
     * 修改服务
     */
    @ApiOperation(value = "修改服务")
    @PostMapping("/editServer")
    public R editServer(@RequestBody AppletServer appletServer) {
        return appletServerService.updateAppletServer(appletServer);
    }

//    /**
//     * 上架服务
//     * @param id
//     * @return
//     */
//    @ApiOperation(value = "上架服务")
//    @GetMapping("/onlineServer/{id}")
//    public R onlineServer(@PathVariable String id) {
//        return appletServerService.onlineProduct(id);
//    }
//
//    /**
//     * 下架服务
//     */
//    @ApiOperation(value = "下架服务")
//    @GetMapping("/offlineServer/{id}")
//    public R offlineServer(@PathVariable String id) {
//        return appletServerService.offlineProduct(id);
//    }

    /**
     * 删除服务
     */
    @ApiOperation(value = "删除服务")
    @PostMapping("/deleteServer")
    public R removeServer(@RequestBody DeleteVo deleteVo) {
        return appletServerService.deleteAppletServerByIds(deleteVo.getIds());
    }

}
